package com.batch.adance.cvs;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

///@Configuration
public class InputFaltFileItemReaderConfigruation {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    @Qualifier("FlatFileWriterDemo")
    private ItemWriter<? super AppInfo> FlatFileWriterDemo;

    @Bean
    public Job FaltFileItemReaderJob() {
        return jobBuilderFactory.get("FaltFileItemReaderJob2")
                .start(FaltFileItemReaderJobStep())
                .build();

    }

    @Bean
    public Step FaltFileItemReaderJobStep() {
        return stepBuilderFactory.get("FaltFileItemReaderJobStep2")
                .<AppInfo, AppInfo>chunk(100)
                .reader(FaltFileReaderDemo())
                .writer(FlatFileWriterDemo)
                .build();
    }

    @Bean
    @StepScope
    public FlatFileItemReader<AppInfo> FaltFileReaderDemo() {
        FlatFileItemReader<AppInfo> reader=new FlatFileItemReader<AppInfo>();
        reader.setResource(new ClassPathResource("um_t_app_info.csv"));
        reader.setLinesToSkip(5);

        DelimitedLineTokenizer tokenizer=new DelimitedLineTokenizer();
        tokenizer.setNames(new String[] 
                {"序号","appid","app_name","app_icon","app_secret","created_time","description"}
        );
        DefaultLineMapper<AppInfo> lineMapper=new DefaultLineMapper<AppInfo>();
        lineMapper.setLineTokenizer(tokenizer);
        lineMapper.setFieldSetMapper(new AppInfoDtoFileMapper());
        lineMapper.afterPropertiesSet();
        reader.setLineMapper(lineMapper);
        return reader;
    }

}